﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace VNSoft.Administrator
{
    public partial class Menu : System.Web.UI.Page
    {
        clsXuLy ex = new clsXuLy();
        SqlParameter[] p;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnCancel_Click(sender, e);
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtTenMenu.Text.Trim().Equals(""))
                gstGetMess("Nhập tên menu", "");
            else
            {
                try
                {
                    p = new SqlParameter[6];
                    p[0] = new SqlParameter("@Ten", txtTenMenu.Text.Trim());
                    p[0].SqlDbType = SqlDbType.NVarChar;
                    p[1] = new SqlParameter("@Menucha", GetGuidDrop(dlMenuCha));
                    p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[2] = new SqlParameter("@Link", txtDuongDan.Text.Trim().Equals("") ? "#" : txtDuongDan.Text.Trim());
                    p[2].SqlDbType = SqlDbType.NVarChar;
                    p[3] = new SqlParameter("@ViTri", double.Parse(txtViTri.Text.Trim().Equals("") ? "0" : txtViTri.Text.Trim()));
                    p[3].SqlDbType = SqlDbType.Float;
                    p[4] = new SqlParameter("@HienThi", ckHienThi.Checked ? 1 : 0);
                    p[4].SqlDbType = SqlDbType.Int;
                    p[5] = new SqlParameter("@ID", Guid.NewGuid());
                    p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                    ex.ExecuteNonQuerry("sp_Menu_Insert", p);
                    
                    btnCancel_Click(sender, e);
                    
                    //gstGetMess("Thêm thành công", "");
                }
                catch (Exception ax)
                {
                    gstGetMess("Lỗi: " + ax.Message, "");
                }
            }
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtTenMenu.Text.Trim().Equals(""))
                gstGetMess("Nhập tên menu", "");
            else
            {
                try
                {
                    p = new SqlParameter[6];
                    p[0] = new SqlParameter("@Ten", txtTenMenu.Text.Trim());
                    p[0].SqlDbType = SqlDbType.NVarChar;
                    p[1] = new SqlParameter("@Menucha", GetGuidDrop(dlMenuCha));
                    p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[2] = new SqlParameter("@Link", txtDuongDan.Text.Trim().Equals("") ? "#" : txtDuongDan.Text.Trim());
                    p[2].SqlDbType = SqlDbType.NVarChar;
                    p[3] = new SqlParameter("@ViTri", double.Parse(txtViTri.Text.Trim().Equals("") ? "0" : txtViTri.Text.Trim()));
                    p[3].SqlDbType = SqlDbType.Float;
                    p[4] = new SqlParameter("@HienThi", ckHienThi.Checked ? 1 : 0);
                    p[4].SqlDbType = SqlDbType.Int;
                    Guid ID = new Guid(hdID.Value);
                    p[5] = new SqlParameter("@ID", ID);
                    p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                    ex.ExecuteNonQuerry("sp_Menu_Update", p);
                    btnCancel_Click(sender, e);
                    
                    //gstGetMess("Cập nhật thành công", "");
                }
                catch (Exception ax)
                {
                    gstGetMess("Lỗi: " + ax.Message, "");
                }
            }
        }
        public object GetGuidDrop(DropDownList dl)
        {
            try
            {
                if (dl.SelectedIndex == 0) return DBNull.Value;
                else
                {
                    Guid ID = new Guid(dl.SelectedValue);
                    return ID;
                }
            }
            catch
            {
                return DBNull.Value;
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            txtTenMenu.Text = "";
            txtDuongDan.Text = "";
            dlMenuCha.SelectedIndex = 0;
            txtViTri.Text = "";
            ckHienThi.Checked = true;
            txtTenMenu.Focus();
            gvMenu.DataSource = ex.GetData("sp_Menu_List");
            gvMenu.DataBind();
            
            LoadMenuCha(dlMenuCha);
            btnAdd.Visible = true;
            btnUpdate.Visible = false;
            ex.Close();
        }
        void LoadMenuCha(DropDownList dl)
        {
            dl.Items.Clear();
            dl.DataSource = ex.GetData("sp_Menu_LoadMenuCha");
            dl.DataBind();
            ex.Close();
            dl.Items.Insert(0, new ListItem("--Chọn menu cha--", ""));
            dl.SelectedIndex = 0;
        }
        protected void gvMenu_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string ID = e.CommandArgument.ToString();
            hdID.Value = ID;
            Guid IDs = new Guid(hdID.Value);
            p = new SqlParameter[1];
            p[0] = new SqlParameter("@ID", IDs);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;

            if (e.CommandName.Equals("Select"))
            {
                DataTable dt = ex.GetData("sp_Menu_ListByID", p);
                if (dt.Rows.Count > 0)
                {
                    txtTenMenu.Text = dt.Rows[0]["Name"].ToString();
                    ckHienThi.Checked = dt.Rows[0]["Active"].ToString().Trim().Equals("1") ? true : false;
                    txtDuongDan.Text = dt.Rows[0]["Link"].ToString();
                    txtViTri.Text = dt.Rows[0]["Postion"].ToString();
                    dlMenuCha.SelectedValue = dt.Rows[0]["MenuParent"].ToString();

                }

                btnUpdate.Visible = true;
                btnAdd.Visible = false;
            }
            else
                if (e.CommandName.Equals("Del"))
                {
                    try
                    {
                        ex.ExecuteNonQuerry("sp_Menu_Del", p);
                        btnCancel_Click(sender, e);
                        //gstGetMess("Xóa thành công", "");
                    }
                    catch (Exception ax)
                    {
                        gstGetMess("Lỗi: " + ax.Message, "");
                    }
                }
        }
        private void gstGetMess(string gstMess, string gstLink)
        {
            if (gstLink == "")
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
            else
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

        }
    }
}